package org.game.quartz;

import java.util.Collection;

import org.game.actor.msg.InternalMsg;
import org.game.cache.OnlinePlayerCache;
import org.game.constant.SecondsJobStatus;
import org.game.db.bean.Player;
import org.game.db.proxy.bean.PlayerProxy;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.xiaoleilu.hutool.log.Log;
import com.xiaoleilu.hutool.log.LogFactory;

import akka.actor.ActorRef;

/**
 * @author : DengYing
 * @CreateDate : 2017年8月24日 下午12:27:59
 * @Description ：每秒定时器
 */
public class SecondsJob implements Job {
	
	static final Log log = LogFactory.get(SecondsJob.class);
	
	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		try {
			Collection<Player> players = OnlinePlayerCache.getAll();
			long nowTime = System.currentTimeMillis();
			for (Player player : players) {
				PlayerProxy playerProxy = player.getPlayerProxy();
				SecondsJobStatus status = playerProxy.getSecondsJobStatus();
				boolean send = false;
				if (status == SecondsJobStatus.INIT || status == SecondsJobStatus.COMPEL) {
					send = true;
				}else if (status == SecondsJobStatus.ACTIVATE) {
					if(nowTime >= playerProxy.getNextSecondsJobTime()){
						send = true;
					}
				}
				
				if (send) {
					player.getPlayerProxy().getActorRef().tell(new InternalMsg("SecondsJob", null),ActorRef.noSender());					
				}
			}
		} catch (Exception e) {
			log.error(e);
		}
	}



}
